package com.dayuanit.dy16.atm.boatm.controller;

import com.dayuanit.dy16.atm.boatm.dto.ResponseDTO;
import com.dayuanit.dy16.atm.boatm.entity.BoUser;
import com.dayuanit.dy16.atm.boatm.exception.BizException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

public abstract class BaseController {
    protected static Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected HttpSession httpSession;

    @ExceptionHandler(BizException.class)
    public ResponseDTO processBizException(BizException be) {
        logger.debug(be.getMessage());
        return ResponseDTO.fail(be.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseDTO processException(Exception e) {
        logger.error(e.getMessage(), e);
        return ResponseDTO.fail("系统错误，请联系客服。");
    }

    protected long getUserId() {
        Subject subject = SecurityUtils.getSubject();
        BoUser user = (BoUser)subject.getSession().getAttribute("loginUser");
        if (user == null) {
            throw new BizException("请登录");
        }
        return user.getId();

//        return 1L;//tom
//        return 3L;//jack
    }

    public void test() {
        System.out.println("this is upexcel branch");
    }

    public void say() {
        System.out.println("this say method in upexcel branch");
    }
}
